<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Common\ResponseCode;
use App\Models\AdminRoleMenuMapModel;
use App\Models\AdminRoleModel;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Validator;

class RoleController extends ApiController {

    /**
     * @param Request $request
     * 角色列表
     */
    public function roleList(Request $request)
    {
        $page = $request->input('page') ? $request->input('page') : 1;
        $pageSize = $request->input('pageSize') ? $request->input('pageSize') : 10;

        $deptList = AdminRoleModel::getRecordListCondition([], ['*'], [], $page, $pageSize);
        $deptCount = AdminRoleModel::getRecordCountCondition();
        return $this->_response(['list' => $deptList, 'page' => $page, 'pageSize' => $pageSize, 'total' => $deptCount]);
    }

    /**
     * @param Request $request
     * 获取编辑页面
     */
    public function getEditInfo(Request $request)
    {
        $rule = [
            'roleId' => 'required|numeric',
        ];
        $messages = [
            'roleId.required' => '权限ID不能为空',
            'roleId.numeric' => '权限ID类型错误',
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->apiResponse(null, ResponseCode::COMMON_ERROR, $error);
        }
        $roleId = $request->input('roleId');
        $roleInfo = AdminRoleModel::findRecordOneCondition(['id' => $roleId]);
        $roleMenuMapList = AdminRoleMenuMapModel::getRecordListCondition(['roleId' => $roleId], ["menuId"])->toArray();
        $roleInfo["roleMenuMap"] = Arr::flatten($roleMenuMapList);
        return $this->_response($roleInfo);

    }

    /**
     * TODO:添加或者修改
     */
    public function addOrEdit(Request $request)
    {
        $roleMap = [];
        $rule = [
            'roleName' => 'required',
            'status' => 'required|numeric',
        ];
        $messages = [
            'roleName.required' => '角色名称不能为空',
            'status.required' => '状态不能为空',
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->_response(null, ResponseCode::COMMON_ERROR, $error);
        }
        $RoleName = $request->input('roleName');
        $Status = $request->input('status');
        $Sort = $request->input('sort');
        $RoleId = $request->input('roleId');
        $MenuIds = $request->post('menuIds');

        $data = [
            'roleName' => $RoleName,
            'status' => $Status,
            'sort' => $Sort,
        ];

        DB::beginTransaction();
        if ($RoleId == -1) {
            //添加并返回角色id
            $inserRoleId = AdminRoleModel::addRecordGetId($data);
            foreach (explode(',', $MenuIds) as $val) {
                $roleMap[] = [
                    'roleId' => $inserRoleId,
                    'menuId' => $val,
                ];
            }
            AdminRoleMenuMapModel::addRecord($roleMap);
            DB::commit();
            return $this->_response(null, ResponseCode::SUCCESS, '添加成功');
        } else {
            AdminRoleModel::updateRecordORM($RoleId, $data);

            AdminRoleMenuMapModel::deleteRecordMass(['RoleId' => $RoleId]);
            foreach (explode(',', $MenuIds) as $val) {
                $roleMap[] = [
                    'roleId' => $RoleId,
                    'menuId' => $val,
                ];
            }
            AdminRoleMenuMapModel::addRecord($roleMap);
            DB::commit();
            return $this->_response(null, ResponseCode::SUCCESS, '修改成功');
        }
    }


    /**
     * @param Request $request
     * 删除
     */
    public function roleDel(Request $request)
    {
        $rule = [
            'roleId' => 'required',
        ];
        $messages = [
            'roleId.required' => '角色id不能为空'
        ];

        $error = Validator::make($request->all(), $rule, $messages)->errors()->first();
        if (!empty($error)) {
            return $this->_response(null, ResponseCode::COMMON_ERROR, $error);
        }

        $RoleId = $request->post('roleId');
        $data = [
            'isDelete' => 1
        ];
        AdminRoleModel::updateRecordORM($RoleId, $data);
        return $this->_response(null, ResponseCode::SUCCESS, '删除成功');
    }
}
